package com.ask.service.businessService.impl;

import com.ask.entity.SeckillVoucher;
import com.ask.entity.Voucher;
import com.ask.mapper.VoucherMapper;
import com.ask.service.businessService.ISecKillVoucherService;
import com.ask.service.businessService.IVoucherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

import static com.ask.constants.RedisConstant.SECKILL_STOCK_KEY;

@Service
public class VoucherServiceImpl extends ServiceImpl<VoucherMapper, Voucher> implements IVoucherService {

    @Autowired
    private ISecKillVoucherService secKillVoucherService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 新增普通优惠卷
     *
     * @param voucher
     * @return
     */
    @Override
    public Voucher addVoucher(Voucher voucher) {
        voucher.setStatus(Voucher.ON_SALE);
        voucher.setCreateTime(LocalDateTime.now());
        voucher.setUpdateTime(LocalDateTime.now());
        save(voucher);
        return voucher;
    }

    /**
     * 新增秒杀优惠卷
     *
     * @param voucher
     */
    @Override
    @Transactional
    public void addSecKillVoucher(Voucher voucher) {
        // 跟新voucher表
        save(voucher);

        // 跟新sec_kill_voucher表
        SeckillVoucher seckillVoucher = new SeckillVoucher();
        BeanUtils.copyProperties(voucher, seckillVoucher);
        seckillVoucher.setVoucherId(voucher.getId());
        secKillVoucherService.save(seckillVoucher);

        Integer stock = voucher.getStock();
        String key = SECKILL_STOCK_KEY + voucher.getId();
        stringRedisTemplate.opsForValue().set(key, stock.toString());
    }

    /**
     * 获取店铺的优惠卷信息
     *
     * @param shopId
     * @return
     */
    @Override
    public List<Voucher> listByShopId(Long shopId) {
        List<Voucher> voucherList = query().eq("shop_id", shopId).list();
        // 如果是秒杀卷，查询sec_kill_voucher秒获取beginTime与endTime
        for (Voucher voucher : voucherList) {
            if (voucher.getType() == 1) {
                SeckillVoucher seckillVoucher = secKillVoucherService.
                        query().eq("voucher_id", voucher.getId()).one();
                if (seckillVoucher != null) {
                    voucher.setBeginTime(seckillVoucher.getBeginTime());
                    voucher.setEndTime(seckillVoucher.getEndTime());
                    voucher.setStock(seckillVoucher.getStock());
                }
            }
        }
        return voucherList;
    }

    /**
     * 查询店铺优惠卷
     *
     * @param shopId
     * @return
     */
    @Override
    public List<Voucher> queryVoucherOfShop(Long shopId) {
        List<Voucher> vouchers = getBaseMapper().queryVoucherOfShop(shopId);
        return vouchers;
    }
}
